package br.ufrpe.bsi.pvs.xml.request;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;

import br.ufrpe.bsi.pvs.control.BSFactory;
import br.ufrpe.bsi.pvs.control.funcionario.FuncionarioBs;
import br.ufrpe.bsi.pvs.model.funcionario.Funcionario;
import br.ufrpe.bsi.pvs.xml.XMLUtil;
import br.ufrpe.bsi.pvs.xml.response.LoggedResponseProcess;

public class LoginRequestProcessor implements RequestProcessor {

	@Override
	public void process(Element request) {
		Element classe = request.getChild(XMLUtil.ELEM_CLASS);
		
			Funcionario f = new Funcionario();
			f.setLogin(classe.getChildText("login"));
			f.setSenha(classe.getChildText("senha"));

			FuncionarioBs bs = BSFactory.getFuncionarioBs();
			bs.logar(f.getLogin(), f.getSenha());

			XMLUtil.writeResponse(LoggedResponseProcess.getLoggedResponseDocument("funcionario"));
		}


	/** generates a default add request xml document */
	public static Document getLoginRequestDocument(String className) {
		Document document = new Document();

		Element root = new Element(XMLUtil.ELEM_ROOT);
		Element request = new Element(XMLUtil.ELEM_REQUEST);

		Attribute nomerequest = new Attribute(XMLUtil.ATTR_NAME, XMLUtil.LOGIN);
		request.setAttribute(nomerequest);
		root.addContent(request);

		Element classe = new Element(XMLUtil.ELEM_CLASS);
		Attribute nomeclasse = new Attribute(XMLUtil.ATTR_NAME, className);
		classe.setAttribute(nomeclasse);

		request.setContent(classe);
		document.setRootElement(root);

		return document;
	}

}
